package com.lc.user.express.adapter;

import android.content.Context;
import android.net.Uri;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.facebook.drawee.view.SimpleDraweeView;
import com.lc.user.express.R;
import com.lc.user.express.app.AppContext;
import com.lc.user.express.httpserver.Net;
import com.lc.user.express.model.DriverModel;
import com.lc.user.express.utils.LocationUtils;
import com.lc.user.express.utils.MapDistance;

import java.util.List;



/**
 * Created by Administrator on 2016/4/6.
 */
public class DriverAdapter extends BaseListAdapter<DriverModel> {
    public DriverAdapter(Context context, List<DriverModel> data) {
        super(context, data);
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder = null;
        DriverModel driverModel = data.get(position);
        if (convertView == null) {
            convertView = LayoutInflater.from(mContext).inflate(R.layout.item_diver_list, null);
            AppContext.ScaleScreenHelper.loadView((ViewGroup) convertView);
            viewHolder = new ViewHolder();
            viewHolder.riv_head = (SimpleDraweeView) convertView.findViewById(R.id.riv_head);
            viewHolder.tv_name = (TextView) convertView.findViewById(R.id.tv_name);
            viewHolder.tv_number = (TextView) convertView.findViewById(R.id.tv_number);
            viewHolder.tv_star = (TextView) convertView.findViewById(R.id.tv_star);
            viewHolder.tv_free = (TextView) convertView.findViewById(R.id.tv_free);
            viewHolder.tv_distance = (TextView) convertView.findViewById(R.id.tv_distance);
            viewHolder.tv_selected = (TextView)convertView.findViewById(R.id.tv_selected);
            convertView.setTag(viewHolder);
        }
        setContentView(convertView, driverModel);
        return convertView;
    }

    private void setContentView(View contentView, DriverModel model) {
        ViewHolder viewHolder = (ViewHolder) contentView.getTag();
        viewHolder.tv_name.setText(model.getName());
        viewHolder.tv_number.setText("近期服务" + model.getNumber() + "次");
        viewHolder.tv_star.setText(model.getStar() + "分");
        viewHolder.tv_distance.setText(MapDistance.getInstance().getLongDistance(LocationUtils.getInstance().lng, LocationUtils.getInstance().lat,model.getLng(),model.getLat()));
        //ImageManager.from(mContext).displayImage(viewHolder.riv_head, Net.SERVER + model.getPicUrl(), R.mipmap.default_pic);
        Log.d(model.getPicUrl(),"222222222222");
        viewHolder.riv_head.setImageURI(Uri.parse(Net.SERVER + model.getPicUrl()));



        if (model.getIsfreemove() == 1) {
            viewHolder.tv_free.setVisibility(View.VISIBLE);
        } else {
            viewHolder.tv_free.setVisibility(View.INVISIBLE);
        }
        if(model.isSelect()){
            viewHolder.tv_selected.setBackgroundColor(mContext.getResources().getColor(R.color.green));
        }else {
            viewHolder.tv_selected.setBackgroundColor(mContext.getResources().getColor(R.color.gray1));
        }
    }

    private class ViewHolder {
        SimpleDraweeView riv_head;
        TextView tv_name;
        TextView tv_number;
        TextView tv_star;
        TextView tv_free;
        TextView tv_distance;
        TextView tv_selected;
    }
}
